package com.nys.cqhttp.message.factory.impl;

import com.alibaba.fastjson.JSONObject;
import com.nys.cqhttp.message.factory.IMessageHandler;

import com.nys.cqhttp.model.Massage;
import com.nys.cqhttp.param.Context;
import com.nys.cqhttp.util.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * DirectUrlMessageHandler
 *
 * @author nysheng
 * @version 1.0.0
 * @description 服务器
 * @date 2021/12/28 19:53
 */
@Slf4j
public class FWQ4MessageHandler implements IMessageHandler {

  @Override
  public List<String> listHandler(Massage messagePojo, Context context) {
    return Arrays.asList(handler(messagePojo, context));
  }

  @Override
  public String handler(Massage messagePojo, Context context) {
    RestTemplate restTemplate = SpringContextUtil.getBean("restTemplate");
    try {
      String[] split = context.getMessageParam().getMessage().split("-");
      String url = messagePojo.getValue().substring(0, messagePojo.getValue().indexOf("/player"));
      List<String> data = new ArrayList<>();
      if (split[0].contains("加管")){
        url += "/player/getDstAdminList";
        JSONObject jsonObject =JSONObject.parseObject(restTemplate.getForEntity(url , String.class).getBody());
        data=jsonObject.getObject("data", List.class);
        if (data.contains(split[1])){
          return "不要重复添加！";
        }
        data.add(split[1]);
      }else if(split[0].contains("加黑")){
        url += "/player/getDstBlacklist";
        JSONObject jsonObject =JSONObject.parseObject(restTemplate.getForEntity(url , String.class).getBody());
        data=jsonObject.getObject("data", List.class);
        if (data.contains(split[1])){
          return "不要重复添加！";
        }
        data.add(split[1]);
      }else if(split[0].contains("加白")){
        url += "/player/getDstWhitelist";
        JSONObject jsonObject =JSONObject.parseObject(restTemplate.getForEntity(url , String.class).getBody());
        data=jsonObject.getObject("data", List.class);
        if (data.contains(split[1])){
          return "不要重复添加！";
        }
        data.add(split[1]);
      }else if (split[0].contains("取管")){
        url += "/player/getDstAdminList";
        JSONObject jsonObject =JSONObject.parseObject(restTemplate.getForEntity(url , String.class).getBody());
        data=jsonObject.getObject("data", List.class);
        if (!data.contains(split[1])){
          return "该用户不是管理员！";
        }
        data.remove(split[1]);
      }else if(split[0].contains("取黑")){
        url += "/player/getDstBlacklist";
        JSONObject jsonObject =JSONObject.parseObject(restTemplate.getForEntity(url , String.class).getBody());
        data=jsonObject.getObject("data", List.class);
        if (!data.contains(split[1])){
          return "该用户不在黑名单！";
        }
        data.remove(split[1]);
      }else if(split[0].contains("加白")){
        url += "/player/getDstWhitelist";
        JSONObject jsonObject =JSONObject.parseObject(restTemplate.getForEntity(url , String.class).getBody());
        data=jsonObject.getObject("data", List.class);
        if (!data.contains(split[1])){
          return "该用户不在白名单！";
        }
        data.remove(split[1]);
      }

      restTemplate.postForEntity(messagePojo.getValue(),data, String.class).getBody();
      return "执行成功";
    } catch (Exception e) {
      return null;
    }
  }
}
